<?php
/**
 *
 * @author sunny5156  <137898350@qq.com>
 * @date 2015-9-11
 * @version
 */
namespace Common\Controller;

use Think\Upload;

use Think\Image;

class BaseAttachmentController extends BaseCommonController{

    public function uploadFile(){
        $model = I('get.model');
    
        $config = array(
            //'maxSize' => 30*1024*1204,
            'rootPath' => ROOT_DIR,
            'savePath' => '/Public/Uploads/',
            //'allowExts' => array('apk')
        );
    
        $upload = new Upload($config); //
        $upload->maxSize = 31457280; // 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        
    
        $info = $upload->upload();
        
    
        if (empty($info)) { // 上传错误提示错误信息
            $this->error($upload->getError());
        } else { // 上传成功 获取上传文件信息
            
            if (!empty($this->_CONFIG['attachs'][$model]['thumb'])) {
                list($w1,$h1) = explode('X', $this->_CONFIG['attachs'][$model]['thumb']);
                $thumbWidth = $w1;
                $thumbHeight = $h1;
            }
            $prefix = '';
            if($thumbHeight && $thumbWidth){
                $prefix = 'thumb_';
                foreach ($info as $v) {
                    // 生成缩略图
                    $image = new Image();
                    $image->open(ROOT_DIR . $v['savepath'] . $v['savename']);
                
                    // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
                    $image->thumb($thumbWidth, $thumbHeight, \Think\Image::IMAGE_THUMB_CENTER)->save(ROOT_DIR . $v['savepath'] . 'thumb_' . $v['savename']);
                    // 生成缩略图
                }
            }
    
            if (count($info) > 1) {
                // @todo
            } else {
                exit("." . $info['Filedata']['savepath'] . $prefix . $info['Filedata']['savename']) ;
            }
        }
    }
    /**
     * base64Upload
     * @author sunny5156  <137898350@qq.com>
     */
    public function base64Upload(){
        $mime = array(
            'image/png' => '.png',
            'image/jpg' => '.jpg',
            'image/jpeg' => '.jpg',
            'image/pjpeg' => '.jpg',
            'image/gif' => '.gif',
            'image/bmp' => '.bmp',
            'image/x-png' => '.png',
        );
        
        $base64 = $_POST['base64'];
        $type = $_POST['type'];
        $imgtype = $mime[$type];
        
        //$dir = dirname(__FILE__);
        
        $dir = ROOT_DIR;
        if($imgtype){
            preg_match('/(.*)base64,(.*)/', $base64, $matches);
            $base64 = $matches['2'];
            $base64 = base64_decode($base64);
            $data = date("Y-m-d");
            $imgname = md5(time().rand(10000,99999));
            //     $imgurl = 'saestor://upload/'.$data.'/'.$imgname.$imgtype;
            $imgurl = '/Public/Uploads/avatar/'.$data.'/';
            if(!file_exists($dir.$imgurl)){
                mkdir($dir.$imgurl,0755,true);
            }
            $imgurl .= $imgname.$imgtype;
            $imgurlname = $data.'/'.$imgname.$imgtype;
            $ress = file_put_contents($dir.$imgurl,$base64);
            if($ress){
                //$st = new SaeStorage();
                $res['status'] = '0';
                //$res['imgurl'] = $st->geturl('upload',$imgurlname);
                $res['imgurl'] = '.'.$imgurl;
            }else{
                $res['status'] = '1';
                $res['msg'] = '上传图片错误，请检查文件夹权限';
            }
        }else{
            $res['status'] = '1';
            $res['msg'] = '格式错误';
        }
        echo json_encode($res);
    }
    
}